package com.google.gson.internal.bind;

import com.google.gson.b.a;
import com.google.gson.b.c;
import com.google.gson.i;
import com.google.gson.j;
import com.google.gson.l;
import com.google.gson.m;
import com.google.gson.p;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

public final class h extends a {
    private static final Reader a = new Reader() {
        public void close() throws IOException {
            throw new AssertionError();
        }

        public int read(char[] cArr, int i, int i2) throws IOException {
            throw new AssertionError();
        }
    };
    private static final Object b = new Object();
    private final List c = new ArrayList();

    public h(j jVar) {
        super(a);
        this.c.add(jVar);
    }

    private void a(c cVar) throws IOException {
        if (f() != cVar) {
            throw new IllegalStateException("Expected " + cVar + " but was " + f());
        }
    }

    private Object p() {
        return this.c.get(this.c.size() - 1);
    }

    private Object q() {
        return this.c.remove(this.c.size() - 1);
    }

    public void a() throws IOException {
        a(c.BEGIN_ARRAY);
        this.c.add(((i) p()).iterator());
    }

    public void b() throws IOException {
        a(c.END_ARRAY);
        q();
        q();
    }

    public void c() throws IOException {
        a(c.BEGIN_OBJECT);
        this.c.add(((m) p()).o().iterator());
    }

    public void close() throws IOException {
        this.c.clear();
        this.c.add(b);
    }

    public void d() throws IOException {
        a(c.END_OBJECT);
        q();
        q();
    }

    public boolean e() throws IOException {
        c f = f();
        return (f == c.END_OBJECT || f == c.END_ARRAY) ? false : true;
    }

    public c f() throws IOException {
        if (this.c.isEmpty()) {
            return c.END_DOCUMENT;
        }
        Object p = p();
        if (p instanceof Iterator) {
            boolean z = this.c.get(this.c.size() - 2) instanceof m;
            Iterator it = (Iterator) p;
            if (!it.hasNext()) {
                return z ? c.END_OBJECT : c.END_ARRAY;
            } else {
                if (z) {
                    return c.NAME;
                }
                this.c.add(it.next());
                return f();
            }
        } else if (p instanceof m) {
            return c.BEGIN_OBJECT;
        } else {
            if (p instanceof i) {
                return c.BEGIN_ARRAY;
            }
            if (p instanceof p) {
                p pVar = (p) p;
                if (pVar.q()) {
                    return c.STRING;
                }
                if (pVar.o()) {
                    return c.BOOLEAN;
                }
                if (pVar.p()) {
                    return c.NUMBER;
                }
                throw new AssertionError();
            } else if (p instanceof l) {
                return c.NULL;
            } else {
                if (p == b) {
                    throw new IllegalStateException("JsonReader is closed");
                }
                throw new AssertionError();
            }
        }
    }

    public String g() throws IOException {
        a(c.NAME);
        Entry entry = (Entry) ((Iterator) p()).next();
        this.c.add(entry.getValue());
        return (String) entry.getKey();
    }

    public String h() throws IOException {
        c f = f();
        if (f == c.STRING || f == c.NUMBER) {
            return ((p) q()).b();
        }
        throw new IllegalStateException("Expected " + c.STRING + " but was " + f);
    }

    public boolean i() throws IOException {
        a(c.BOOLEAN);
        return ((p) q()).f();
    }

    public void j() throws IOException {
        a(c.NULL);
        q();
    }

    public double k() throws IOException {
        c f = f();
        if (f == c.NUMBER || f == c.STRING) {
            double c = ((p) p()).c();
            if (o() || !(Double.isNaN(c) || Double.isInfinite(c))) {
                q();
                return c;
            }
            throw new NumberFormatException("JSON forbids NaN and infinities: " + c);
        }
        throw new IllegalStateException("Expected " + c.NUMBER + " but was " + f);
    }

    public long l() throws IOException {
        c f = f();
        if (f == c.NUMBER || f == c.STRING) {
            long d = ((p) p()).d();
            q();
            return d;
        }
        throw new IllegalStateException("Expected " + c.NUMBER + " but was " + f);
    }

    public int m() throws IOException {
        c f = f();
        if (f == c.NUMBER || f == c.STRING) {
            int e = ((p) p()).e();
            q();
            return e;
        }
        throw new IllegalStateException("Expected " + c.NUMBER + " but was " + f);
    }

    public void n() throws IOException {
        if (f() == c.NAME) {
            g();
        } else {
            q();
        }
    }

    public String toString() {
        return getClass().getSimpleName();
    }
}
